
const { timeLog } = require("console");
let query = require("../model/connect")


const express = require("express");
let router = express.Router();
const fs = require("fs");
const path = require("path");

// 1.添加插旗记录addflag
// 2.获取插旗记录getflag
// 3.获取插旗记录详细信息getDetail
// 添加记录
router.post("/addflag", async (req, res) => {
    try {
        let { uid, imgs, content,ip,title } = req.body;
        let addtime = new Date().getTime();
        // 添加插旗记录  flag
        let result = await query(`INSERT INTO flag(uid,addtime,ip,bgimg) VALUES(${uid},${addtime},'${ip}','${imgs[0]}')`);

        let fid = result.insertId; // 添加的新数据的id值；

        // 添加插旗详细记录   flag_detail
        await query(`INSERT INTO flag_detail(fid,title,content) VALUES(${fid},'${title}','${content}')`)
        // 添加插旗记录图片图片     flag_detail_imgs
        for (let i = 0; i < imgs.length; i++) {
            await query(`INSERT INTO flag_detail_imgs(fid,imgurl) VALUES(${fid},'${imgs[i]}')`);
        }

        res.json({
            info: "添加成功",
            status: 1
        })
    } catch (err) {
        // console.log(err);
        res.json({
            info: "添加失败",
            status: 0
        })
    }
})

// 获取记录
router.get("/getflag", async (req, res) => {
    try {
        
        let { uid, p } = req.query;
        let perpage = 4;
        // 获取封面，时间，用户头像
        str = `SELECT A.id,A.uid,A.addtime,A.bgimg,B.avatar,B.username FROM flag A LEFT JOIN user B ON A.uid=B.id ORDER BY A.addtime LIMIT ${(p - 1) * perpage},${perpage} `
        let result = await query(str);
        for (let i = 0; i < result.length; i++) {
            result[i].addtime = new Date(result[i].addtime).getFullYear() + "." + (new Date(result[i].addtime).getMonth() + 1) + "." + new Date(result[i].addtime).getDate();
        }
        res.json({
            info: "获取成功",
            status: 1,
            result
        })
    } catch (err) {
        res.json({
            info: "获取失败",
            status: 2,
            err
        })
    }
})
//获取自己的记录
router.get("/getmyflag", async (req, res) => {
    try {
        
        let { uid, p } = req.query;
        let perpage = 4;
        // 获取封面，时间，用户头像
        str = `SELECT A.id,A.uid,A.addtime,A.bgimg,B.avatar,B.username FROM flag A LEFT JOIN user B ON A.uid=B.id WHERE A.uid=${uid} ORDER BY A.addtime LIMIT ${(p - 1) * perpage},${perpage} `
        let result = await query(str);
        for (let i = 0; i < result.length; i++) {
            result[i].addtime = new Date(result[i].addtime).getFullYear() + "." + (new Date(result[i].addtime).getMonth() + 1) + "." + new Date(result[i].addtime).getDate();
        }
        res.json({
            info: "获取成功",
            status: 1,
            result
        })
    } catch (err) {
        res.json({
            info: "获取失败",
            status: 2,
            err
        })
    }
})

router.get("/getflagtop", async (req, res) => {
    try {
        
        let {uid} = req.query;
        // 获取封面，时间，用户头像
        str = `SELECT A.id,B.avatar,B.username FROM flag A LEFT JOIN user B ON A.uid=B.id ORDER BY A.addtime LIMIT 0,3`
        let result = await query(str);
        for (let i = 0; i < result.length; i++) {
            result[i].addtime = new Date(result[i].addtime).getFullYear() + "." + (new Date(result[i].addtime).getMonth() + 1) + "." + new Date(result[i].addtime).getDate();
        }
        res.json({
            info: "获取成功",
            status: 1,
            result
        })
    } catch (err) {
        res.json({
            info: "获取失败",
            status: 2,
            err
        })
    }
})

router.get("/getDetail", async (req, res) => {
    try {
        let { fid } = req.query;
        //获取时间，地点，用户名，头像
        str1=`SELECT A.uid,A.addtime,A.ip,B.username,B.avatar,C.title,C.content FROM flag A INNER JOIN user B ON A.uid=B.id INNER JOIN flag_detail C ON A.id=C.fid WHERE A.id=${fid}`
        let result=await query(str1);
        result[0].addtime = new Date(result[0].addtime).getFullYear() + "." + (new Date(result[0].addtime).getMonth() + 1) + "." + new Date(result[0].addtime).getDate();
    
        
        //从flag_detail_imgs表中获取图片 
        let result2=await query(`SELECT imgurl FROM flag_detail_imgs WHERE flag_detail_imgs.fid=${fid}`)
        result[0].imgs=result2.map(item=>item.imgurl);
        // console.log(result);
        res.json({
            info: "返还成功",
            list: result,
            status: 1
        })
    } catch (err) {
        // console.log("??", err);
        res.json({
            info: "返还失败",
            status: 0
        })
    }
})

module.exports = router;